(function($) {
  'use strict';

	$(function() {
		var $fullText = $('.admin-fullText');
		$('#admin-fullscreen').on('click', function() {
  			$.AMUI.fullscreen.toggle();
	 	});
	
	    $(document).on($.AMUI.fullscreen.raw.fullscreenchange, function() {
	  		$fullText.text($.AMUI.fullscreen.isFullscreen ? '退出全屏' : '开启全屏');
	    });
	}); 
})(jQuery);

	//上传图片
	$(".fileScreen").on("change", function(){
		var _this = $(this);
	    var files = !!this.files ? this.files : [];
	    if (!files.length || !window.FileReader) {
	    	//add codes here...
	    	return false;
	    };
	    if (/^image/.test( files[0].type)){
		    var reader = new FileReader();
		    reader.readAsDataURL(files[0]);
		    reader.onloadend = function(){
		    	var base64 = reader.result;  
			    var newBase64 = base64.replace(/\+/g, "%2B");  
				_this.siblings("img").attr("src",this.result).show().removeClass("hide");
		  		_this.closest('.file-box').css({'background':'#f5f5f5','border':'2px dashed #ccc'})
	    	}
	    }
	});	
	
	//form
	function createForm(parent, param, cb){
		var fieldset = document.createElement("fieldset");
		parent.append(fieldset);
		$(parent).append(fieldset);
		
		$.each(param,function(i,config){
			config.parent = fieldset;
			if(config.type == "textInput"){
				createTextInput(config);
			}else if(config.type == "numberInput"){
				createNumberInput(config);
			}else if(config.type == "passwordInput"){
				createPasswordInput(config);
			}else if(config.type == "imageUpload"){
				createImageUpload(config);
			}else if(config.type == "fileUpload"){
				createFileUpload(config);
			}else if(config.type == "checkbox"){
				createCheckbox(config);
			}else if(config.type == "checkInline"){
				createCheckboxInline(config);
			}else if(config.type == "radio"){
				createRadio(config);
			}else if(config.type == "radioInline"){
				createRadioInline(config);
			}else if(config.type == "select"){
				createSelect(config);
			}else if(config.type == "multiSelect"){
				createMultiSelect(config);
			}else if(config.type == "textarea"){
				createTextarea(config);
			}else{
				console.log("表单类型不存在");
			}
		});
		return typeof cb == 'function' && cb();
	}
	
	//创建普通文本输入框
	function createTextInput(config){
		var outDiv = document.createElement("div");
		var $outDiv = $(outDiv); 

		$outDiv.addClass("an-form-ground");
		
		var html = '';
		html += "<label for='"+config.id+"'>"+config.label+"</label>";
		html += "<input type='text' class='"+config.className+"' id='"+config.id+"' placeholder='"+config.placeholder+"'>";
		
		$outDiv.append(html);
		
		$(config.parent).append($outDiv);
	}
	
	//创建数字输入框
	function createNumberInput(config){
		var outDiv = document.createElement("div");
		var $outDiv = $(outDiv); 

		$outDiv.addClass("an-form-ground");
		
		var html = '';
		html += "<label for='"+config.id+"'>"+config.label+"</label>";
		html += "<input type='number' class='"+config.className+"' id='"+config.id+"' placeholder='"+config.placeholder+"'>";
		
		$outDiv.append(html);
		
		$(config.parent).append($outDiv);
	}
	
	//创建密码输入框
	function createPasswordInput(config){
		var outDiv = document.createElement("div");
		var $outDiv = $(outDiv); 

		$outDiv.addClass("an-form-ground");
		
		var html = '';
		html += "<label for='"+config.id+"'>"+config.label+"</label>";
		html += "<input type='password' class='"+config.className+"' id='"+config.id+"' placeholder='"+config.placeholder+"'>";
		
		$outDiv.append(html);
		
		$(config.parent).append($outDiv);
	}
	
	//创建图片上传框
	function createImageUpload(config){
		var outDiv = document.createElement("div");
		var $outDiv = $(outDiv); 

		$outDiv.addClass("an-form-ground");
		
		var html = '';
		html += "<label for='"+config.id+"'>"+config.label+"</label>";
		html += "<div class='file-box'><img id='preview-img' src='../../assets/img/icon_add.png' class='hide'>";
		html += "<input  type='file' class='fileScreen' name='fileScreen' id='"+config.id+"' accept='image/*' value=''>";
		html += "</div>	";
		$outDiv.append(html);
		
		$(config.parent).append($outDiv);
	}
	
	//创建文件上传框
	function createFileUpload(config){
		var outDiv = document.createElement("div");
		var $outDiv = $(outDiv); 

		$outDiv.addClass("an-form-ground");
		$outDiv.addClass("an-form-file");
		
		var html = '';
		html += "<label for='"+config.id+"'>"+config.label+"</label>";
		html += "<div><button type='button' class='am-btn am-btn-default am-btn-sm'>";
		html += "<i class='am-icon-cloud-upload'></i> 选择要上传的文件</button>";
		html += "</div>	";
		html += "<input type='file' id='"+config.id+"'>";
		$outDiv.append(html);
		
		$(config.parent).append($outDiv);
	}
	
	//创建垂直复选框
	function createCheckbox(config){
		var outDiv = document.createElement("div");
		var $outDiv = $(outDiv); 

		$outDiv.addClass("am-checkbox");
		
		var html = '';
			html += "<label>";
			html += "<input type='checkbox' value='"+config.value+"' name='"+config.name+"'>"+config.text;
			html += "</label>";
		$outDiv.append(html);
		
		$(config.parent).append($outDiv);
	}
	
	
	//创建水平复选框
	function createCheckboxInline(config){
		var outDiv = document.createElement("div");
		var $outDiv = $(outDiv); 

		$outDiv.addClass("am-form-group");
		
		var html = '';
		html += "<label class='checkboxTitle'>"+config.title+"</label>"
		for(var i=0; i<config.options.length; i++){
			html += "<label class='am-checkbox-inline'>";
			html += "<input type='checkbox' value='"+config.options[i].value+"' name='"+config.name+"'>"+config.options[i].text;
			html += "</label>";
		}
		$outDiv.append(html);
		
		$(config.parent).append($outDiv);
	}
	
	//创建垂直单选框
	function createRadio(config){
		$(config.parent).append("<label class='radioTitle'>"+config.title+"</label>");
		for(var i=0; i<config.options.length; i++){
			var outDiv = document.createElement("div");
			var $outDiv = $(outDiv); 
			$outDiv.addClass("am-radio");
			
			var html = '';
			html += "<label>";
			if(i==0){
				html += "<input type='radio' value='"+config.options[i].value+"' name='"+config.name+"' checked>"+config.options[i].text;
			}else{
				html += "<input type='radio' value='"+config.options[i].value+"' name='"+config.name+"'>"+config.options[i].text;
			}
			html += "</label>";
			
			$outDiv.append(html);
			$(config.parent).append($outDiv);
		}
	}
	
	//创建水平单选框
	function createRadioInline(config){
		var outDiv = document.createElement("div");
		var $outDiv = $(outDiv); 
		
		$outDiv.addClass("am-form-group");
		
		var html = '';
		html += "<label class='radioTitle'>"+config.title+"</label>"
		for(var i=0; i<config.options.length; i++){
			html += "<label class='am-radio-inline'>";
			if(i==0){
				html += "<input type='radio' value='"+config.options[i].value+"' name='"+config.name+"' checked>"+config.options[i].text;
			}else{
				html += "<input type='radio' value='"+config.options[i].value+"' name='"+config.name+"'>"+config.options[i].text;
			}
			html += "</label>";
		}
		$outDiv.append(html);
		$(config.parent).append($outDiv);
	}
	
	//创建下拉框
	function createSelect(config){
		var outDiv = document.createElement("div");
		var $outDiv = $(outDiv); 
		
		$outDiv.addClass("am-form-group");
		var html = '';
		html += "<label class='"+config.id+"'>"+config.label+"</label>";
		html += "<select id='"+config.id+"'>";
		
		for(var i=0; i<config.options.length; i++){
			html += "<option value='"+config.options[i].value+"'>"+config.options[i].text+"</option>";
		}
		
		html += "</select><span class='am-form-caret'></span>";
		
		$outDiv.append(html);
		$(config.parent).append($outDiv);
	}
	
	//创建多选框
	function createMultiSelect(config){
		var outDiv = document.createElement("div");
		var $outDiv = $(outDiv); 
		
		$outDiv.addClass("am-form-group");
		var html = '';
		html += "<label class='"+config.id+"'>"+config.label+"</label>";
		html += "<select multiple id='"+config.id+"'>";
		
		for(var i=0; i<config.options.length; i++){
			html += "<option value='"+config.options[i].value+"'>"+config.options[i].text+"</option>";
		}
		
		html += "</select>";
		
		$outDiv.append(html);
		$(config.parent).append($outDiv);
	}
	
	//创建文本域
	function createTextarea(config){
		var outDiv = document.createElement("div");
		var $outDiv = $(outDiv); 

		$outDiv.addClass("am-form-group");
		
		var html = '';
		html += "<label class='"+config.id+"'>"+config.label+"</label>";
		html += "<textarea class='"+config.id+"' rows='"+config.row+"' id='"+config.id+"'></textarea>";
		$outDiv.append(html);
		
		$(config.parent).append($outDiv);
	}
	
	function createMenu(){
		$(".menu dd").hide();
		$(".menu dt").click(function(){
				//如果该列表已经展开
				if($(this).hasClass("active")){
					$(this).removeClass("active");
					$(this).parent().find('img').attr("src","../../assets/img/icon_rightArrow.png");
				}else{
					$(this).addClass("active");
					$(this).parent().find('img').attr("src","../../assets/img/icon_downArrow.png");
				}
				$(this).parent().find('dd').slideToggle();
		});
	}
	
	function createSideMenu(result,param){
		var ul = document.createElement("ul");
    	var $ul = $(ul);
    	$ul.addClass(param.className);
		$ul.addClass("sidebar-nav");
		$ul.attr('id', param.id);
		
		var html = "";
		var menu1 = [];
		var menu2 = [];
		$.each(result.datas,function(i,item){
			if(item[param.rootField] =='' ){//填充一级菜单
				menu1.push(item);return true;
			}else{
				menu2.push(item);return true;
			}
		});
		//构建左菜单
		html += "<li class='sidebar-nav-heading' ><span class='sidebar-nav-heading-info' >" + param.title + "</span></li>";
        
		$.each(menu1,function(i,item){//填充一级菜单				
		    html += "<li class='sidebar-nav-link' >";	
		    html += "<a href='javascript:;' class='sidebar-nav-sub-title'>"
                      + item[param.displayField] 
                      + "<span class='am-icon-chevron-down am-fr am-margin-right-sm sidebar-nav-sub-ico'></span></a>";
			$.each(menu2,function(j,item1){//遍历取出二级菜单
				if(j==0){
					html += "<ul class='sidebar-nav sidebar-nav-sub'>";
				}
	            if(item1[param.rootField] == item[param.idField]){//二级左菜单字符
	                html += "<li class='sidebar-nav-link " + item1[param.idField] + "'><a href='" + item1[param.urlField] + "'><span class='am-icon-angle-right sidebar-nav-link-logo'></span>" + item1[param.displayField] + "</a></li>";
	            }
			});	
			html += "</ul>";
			html += "</li>";
        });
        $ul.append(html);
	    $("#"+param.menuParent).append($ul);
	    $("#"+param.button).on('click',function(){
			$(".sidebar-nav").toggle("normal");
	    });

		if($(window).width() < 1024){
			$(".sidebar-nav").toggle("normal");
		}
			
		// 侧边菜单
		$('.sidebar-nav-sub-title').on('click', function() {
		    $(this).siblings('.sidebar-nav-sub').slideToggle(80)
		        .end()
		        .find('.sidebar-nav-sub-ico').toggleClass('sidebar-nav-sub-ico-rotate');
		})
	}